package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC828 {
    public long startExecuteTime = System.currentTimeMillis();

    /*
     * @param 此题目参考了别人代码
     * 这是因为问题情况较为复杂
     * 开玩笑，hard难度能允许你随便想到吗
     * 未来需要再次复习此道题目
     * @return:
     */

    class Solution {
        public int uniqueLetterString(String S) {
            Map<Character, List<Integer>> index = new HashMap();
            for (int i = 0; i < S.length(); ++i) {
                char c = S.charAt(i);
                index.computeIfAbsent(c, x -> new ArrayList<Integer>()).add(i);
            }

            long ans = 0;
            for (List<Integer> A : index.values()) {
                for (int i = 0; i < A.size(); ++i) {
                    long prev = i > 0 ? A.get(i - 1) : -1;
                    long next = i < A.size() - 1 ? A.get(i + 1) : S.length();
                    ans += (A.get(i) - prev) * (next - A.get(i));
                }
            }

            return (int) ans % 1_000_000_007;
        }
    }

    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC828 an = new LC828();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
